package com.game.serivce;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import net.SocketEnum;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.component.Util;
import com.component.code.Constant;
import com.component.code.TishiManager;
import com.component.mode.Button;
import com.data.WDataConversion;
import com.data.model.PlayerDiankong;
import com.data.model.PlayerGameInfo;
import com.data.serivce.Impl.PlayerDBService;
import com.data.spring.BeanFactory;
import com.data.third.DataService;
import com.dictionary.DictionaryTabel;
import com.game.model.Desk;
import com.game.model.DeskPlayer;
import com.game.model.FactoryPai;
import com.game.model.Pai;
import com.game.rule.Rule;

/**
 * 入座玩家服务(包含机器人)
 * @author wuxiaojian
 *
 */
public class DeskPlayerService {
	//private Desk desk;
	public RoomDeskService roomDeskService;
	public PlayerService playerService;
	public DeskPlayerService(JSONObject json){
		 this.playerService = new PlayerService(json);
	}
	//private boolean isAuto = false;
	private LinkedHashMap<Integer, Pai> paiMap = new LinkedHashMap<>();

	private ArrayList<String> bonusAwardList = new ArrayList<>();

	private int myYaXian = 2400;

	public long curAwardFen = 0;
	private int curBetIndex = -1;
	public long[] betArea = null;
	public int[] autobetNumArea = null;
	public long playerNowBossFen = 0;
	public long bonusFen = 0;
	//public int autoCount = 0;
	public long bossFenT = 0;
	
	public int freeOpenAwardCount = -1;
	public int totalFreeCount = -1;
	public boolean freeFlag = false;
	boolean bonusGameFlag = false;

	static Logger logger = Logger.getLogger(DeskPlayerService.class.getName());

	public void initYaXian() {}

	public void initBetArea() {
		String betAreaStr = getRoomDeskService().getConfigRoomService().betArea;
		String[] betAreaArr = betAreaStr.split(",");
		betArea = new long[betAreaArr.length];
		for (int i = 0; i < betArea.length; i++) {
			if (i == 0) {
				curBetIndex = i;
			}
			betArea[i] = Long.valueOf(betAreaArr[i]);
		}
	}

	public void initBonusAward() {
		bonusAwardList.add("1");

		bonusAwardList.add("2:10");
		bonusAwardList.add("3:5");
		bonusAwardList.add("2:5");
		bonusAwardList.add("3:10");

		bonusAwardList.add("2:10");
		bonusAwardList.add("3:5");
		bonusAwardList.add("2:5");

		bonusAwardList.add("4");

		bonusAwardList.add("2:10");
		bonusAwardList.add("3:5");
		bonusAwardList.add("2:5");
		bonusAwardList.add("3:10");

		bonusAwardList.add("4");

		bonusAwardList.add("2:10");
		bonusAwardList.add("3:5");
	}

//	public void initAutoBetNumArea() {
//		String autoBetNumAreaStr = getRoomDeskService().getConfigRoomService().autoBetNumArea;
//		String[] betAutoNumAreaArr = autoBetNumAreaStr.split(",");
//		autobetNumArea = new int[betAutoNumAreaArr.length];
//		for (int i = 0; i < autobetNumArea.length; i++) {
//			autobetNumArea[i] = Integer.valueOf(betAutoNumAreaArr[i]);
//		}
//	}

//	public boolean isAuto() {
//		return isAuto;
//	}
//
//	public void setAuto(boolean isAuto) {
//		this.isAuto = isAuto;
//	}

	public DeskPlayerService() {
	}

	public void initDeskPlayerService(DeskPlayerService dps) {
		//setRoomDeskService(dps.roomDeskService);
		//desk = new Desk();
		//desk.setDeskNum(dps.deskPlayer.getDeskNum());
		initPaiMap(true);
		//initYaXian();
		initBetArea();
		//initBonusAward();
		//initAutoBetNumArea();
		setGameState(DictionaryTabel.Game_Sleep, false);
		setTimerTask();
	}

	long ying = 0;

	public void game_beginJu() {
		System.out.println("game_beginJu");
		setGameState(DictionaryTabel.Game_BeginJu, false);
		checkDianKong(0);// 检查点控信息
		try {
			isJieSuan = false;
			// System.out.println("剩余免费次数：" + freeOpenAwardCount);
			getRoomDeskService().autoReSetShuZhi(betArea[curBetIndex], freeOpenAwardCount > 0);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
			return;
		}
		sendGameResult();
	}

	public void initPaiMap(boolean isHaveFree) {
//		paiMap.put(0 + "", new Pai(0, "A", 0, 0.2, 0.4, 1.2,3.6,18,90));
//		paiMap.put(1 + "", new Pai(1, "B", 0, 0.3, 0.6, 1.8,5.4,27,135));
//		paiMap.put(2 + "", new Pai(2, "C", 0, 0.4, 0.8, 2.4,7.2,34.75,180));
//		paiMap.put(3 + "", new Pai(3, "D", 0, 0.4, 0.8, 2.4,7.2,34.75,180));
//		paiMap.put(4 + "", new Pai(4, "E", 0.5, 1, 2, 6,18,90,450));
//		paiMap.put(5 + "", new Pai(5, "F", 0.5, 1, 2, 6,18,90,450));
//		paiMap.put(6 + "", new Pai(6, "G", 0.6, 1.2, 2.4, 7.2,21.6,108,415));
//		paiMap.put(7 + "", new Pai(7, "H", 0.7, 1.4, 2.8, 8.4,25.2,126,630));
//		paiMap.put(8 + "", new Pai(8, "I", 0.8, 1.6, 3.2, 9.6,28.8,144,720));
//		
//		paiMap.put(0, new Pai(4, "E", 0.5, 1, 2, 6,18,90,450));
//		paiMap.put(1, new Pai(4, "E", 0.5, 1, 2, 6,18,90,450));
//		paiMap.put(2, new Pai(4, "E", 0.5, 1, 2, 6,18,90,450));
//		paiMap.put(3, new Pai(3, "D", 0, 0.4, 0.8, 2.4,7.2,34.75,180));
//		paiMap.put(4, new Pai(4, "E", 0.5, 1, 2, 6,18,90,450));
//		paiMap.put(5, new Pai(12, "I", 0, 0, 0, 0,0,0,0));
//		paiMap.put(6, new Pai(12, "I", 0, 0, 0, 0,0,0,0));
//		paiMap.put(7, new Pai(12, "I", 0, 0, 0, 0,0,0,0));
//		paiMap.put(8, new Pai(12, "I", 0, 0, 0, 0,0,0,0));
		for (int j = 0; j < 9; j++) {
			float randomValue = (float) (Math.random());
			paiMap.put(
					j,
					FactoryPai.getInstance().getOneCharacterByRandomValue(randomValue, getRoomDeskService().getConfigRoomService().randomRate,
							getRoomDeskService().getConfigRoomService().totalRate, isHaveFree));
		}
		// 0=A 1=K 2=O 3=J 4=钥匙 5=罐子 6=金字塔 7=法老 8=狗头 9=艳后 10=黑猫(BOUNS)
		// 11=木乃伊(WILD)
		// 12=眼睛(SCATTER)
		// paiMap.put(0 + "", FactoryPai.getInstance().getOnePaiByType(0,
		// isHaveFree));
		// paiMap.put(1 + "", FactoryPai.getInstance().getOnePaiByType(12,
		// isHaveFree));
		// paiMap.put(2 + "", FactoryPai.getInstance().getOnePaiByType(1,
		// isHaveFree));
		//
		// paiMap.put(3 + "", FactoryPai.getInstance().getOnePaiByType(12,
		// isHaveFree));
		// paiMap.put(4 + "", FactoryPai.getInstance().getOnePaiByType(2,
		// isHaveFree));
		// paiMap.put(5 + "", FactoryPai.getInstance().getOnePaiByType(3,
		// isHaveFree));
		//
		// paiMap.put(6 + "", FactoryPai.getInstance().getOnePaiByType(4,
		// isHaveFree));
		// paiMap.put(7 + "", FactoryPai.getInstance().getOnePaiByType(5,
		// isHaveFree));
		// paiMap.put(8 + "", FactoryPai.getInstance().getOnePaiByType(12,
		// isHaveFree));
		//
		// paiMap.put(9 + "", FactoryPai.getInstance().getOnePaiByType(7,
		// isHaveFree));
		// paiMap.put(10 + "", FactoryPai.getInstance().getOnePaiByType(8,
		// isHaveFree));
		// paiMap.put(11 + "", FactoryPai.getInstance().getOnePaiByType(8,
		// isHaveFree));
		//
		// paiMap.put(12 + "", FactoryPai.getInstance().getOnePaiByType(9,
		// isHaveFree));
		// paiMap.put(13 + "", FactoryPai.getInstance().getOnePaiByType(9,
		// isHaveFree));
		// paiMap.put(14 + "", FactoryPai.getInstance().getOnePaiByType(12,
		// isHaveFree));

	}


	public void exit(String reason) {
		try {
			if (playerService.getPlayer().getSession() != null) {
				playerService.getPlayer().getSession().removeAttribute("deskNum");
				playerService.getPlayer().getSession().removeAttribute("azimuth");
				playerService.getPlayer().getSession().removeAttribute("roomNum");
				playerService.getPlayer().setSession(null);
			}
			exiting(reason);
		} catch (Exception e) {
			e.printStackTrace();
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
		}
	}


	public void exiting(String reason) {
		// 玩家退出情况
		// if (curAwardFen > 0) {
		game_Result();
		// }
		PlayerService playerService = this.playerService;
		if (playerService != null) {
			try {
				Long playerId = playerService.getPlayer().getPlayerId();
				if (playerService.getPlayer().getSession() != null) {
					playerService.getPlayer().getSession().removeAttribute("deskNum");
					playerService.getPlayer().getSession().removeAttribute("azimuth");
					playerService.getPlayer().getSession().removeAttribute("roomNum");
				}
				DataService.leaveDesk(playerId, getRoomDeskService().getConfigRoomService().gameNumberForDb,
						getRoomDeskService().getConfigRoomService().roomNumberForDb, getRoomDeskService().getConfigDesk().getDeskNum(), 0, 2);
				DataService.leaveRoom(playerId, getRoomDeskService().getConfigRoomService().gameNumberForDb,
						getRoomDeskService().getConfigRoomService().roomNumberForDb);
//				MyPrint.print(playerService.getPlayer().getPlayerId() + "exit reason--->" + reason);
//				MyPrint.print(playerService.getPlayer().getPlayerId() + "exit desk success!");
//				MyPrint.print(playerService.getPlayer().getPlayerId() + "exit room success!");
				if (getRoomDeskService().getConfigDesk().getGameState() != DictionaryTabel.Game_Close && !getRoomDeskService().getConfigDesk().isClosing()) {
					setGameState(DictionaryTabel.Game_Sleep, false);
				}
				initBetArea();
				clearData();
				freeOpenAwardCount = -1;
				freeFlag = false;
				//autoCount = 0;
				//setAuto(false);
				playerService = null;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				clearData();
				setGameState(DictionaryTabel.Game_Sleep, false);
			}
		}
	}

	/** 得到桌子上玩家的数量（不包括电脑） **/
	public int getRealPlayersCount() {
		int c = 0;
		if (playerService != null) {
			c++;
		}
		return c;
	}

//	public boolean joinDesk(final PlayerService ps) {
//		// TODO Auto-generated method stub
//		if (playerService == null) {
//			try {
//				initBetArea();
//				//initAutoBetNumArea();
//				ps.getPlayer().getSession().setAttribute("deskNum", getDesk().getDeskNum());
//				ps.getPlayer().getSession().setAttribute("roomNum", getRoomDeskService().getRoom().getConfigId());
//				//MyPrint.print(ps.getPlayer().getPlayerId() + "joinDesk success!desk number--->" + getDesk().getDeskNum());
//				ps.getPlayer().setRoomNum(getRoomDeskService().getConfigRoomService().roomNumberForDb);
//				ps.getPlayer().setDeskNum(getDesk().getDeskNum());
//				setPlayerService(ps);
//				// if (getRoomDeskService().getConfigDesk().getGameState() == DictionaryTabel.Game_Sleep) {
//				setGameState(DictionaryTabel.Game_Wait, false);
//				// }
//				JSONObject messageJson = getMessageJson("initDeskI");
//				ps.sendMessages(SocketEnum.initDeskI, messageJson.toString());
//				// -------------------------
//				Long playerId = ps.getPlayer().getPlayerId();
//				DataService
//						.intoRoom(playerId, getRoomDeskService().getConfigRoomService().gameNumberForDb, getRoomDeskService().getConfigRoomService().roomNumberForDb);
//				DataService.intoDesk(playerId, getRoomDeskService().getConfigRoomService().gameNumberForDb,
//						getRoomDeskService().getConfigRoomService().roomNumberForDb, getDesk().getDeskNum(), 0);
//				return true;
//			} catch (Exception e) {
//				// TODO: handle exception
//				e.printStackTrace();
//				clearData();
//				setGameState(DictionaryTabel.Game_Sleep, false);
//			}
//
//		}
//		return false;
//
//	}

	public JSONObject getMessageJson(String type) {
		JSONObject jsonObject = new JSONObject();
		switch (type) {
		case "initDeskI":
			long deskId = getRoomDeskService().getConfigDesk().getDeskNum() + 1;
			jsonObject.put("deskId", deskId);
			int roomId = getRoomDeskService().getConfigRoomService().roomNumberForDb + 1;
			jsonObject.put("roomId", roomId);
			jsonObject.put("betArea", JSONArray.fromObject(betArea));
			//jsonObject.put("autoBetNumArea", JSONArray.fromObject(autobetNumArea));
			jsonObject.put("myMoney", playerService.getPropNum(2));
			jsonObject.put("myName", playerService.getPlayer().getNickName());
			jsonObject.put("myVip", playerService.getPlayer().getVipLevel());
			jsonObject.put("myHeadUrl", playerService.getPlayer().getHeadUrl());
			//jsonObject.put("myYaXian", myYaXian);
			// jsonObject.put("roomConfig",
			// JSONObject.fromObject(getRoomDeskService().getConfigRoomService()).toString());
			break;
		}
		return jsonObject;
	}

	public ScheduledExecutorService scheduExec = Executors.newSingleThreadScheduledExecutor();

	public void setTimerTask() {
		Runnable task = new Runnable() {
			public void run() {
				try {
					if (getRoomDeskService().getConfigDesk().getGameState() > DictionaryTabel.Game_Sleep) {
						deal();
						// debug();11
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
					clearData();
					setGameState(DictionaryTabel.Game_Sleep, false);
					logger.error("commonExceotion", e);
				}
			}
		};
		scheduExec.scheduleWithFixedDelay(task, 0, 500, TimeUnit.MILLISECONDS);
	}

	public synchronized void deal() {
		try {
			getRoomDeskService().getConfigDesk().setRecordRunTime(getRoomDeskService().getConfigDesk().getRecordRunTime() + 1);
			switch (getRoomDeskService().getConfigDesk().getGameState()) {
			case DictionaryTabel.Game_Result:
				game_Result();
				break;
			case DictionaryTabel.Game_nextRank:
				game_BONUSWait();
				break;
			case DictionaryTabel.Game_Wait:
				if (freeOpenAwardCount <= 0) {
					checkMoney();
				}
				break;
			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("commonExceotion", e);
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
		}

	}

	public void checkMoney() {
		try {
			if (playerService != null) {
				JSONObject jsonObject = Util.isPassEntryLimit(playerService.getPlayer(),
						roomDeskService.getConfigRoomService().roomLimitMoney);
				if (!jsonObject.getBoolean("flag")) {
					playerService.sendMessage(SocketEnum.infoI, Constant.getTishiT(jsonObject.getString("msg"), 1, new ArrayList<Button>() {
						{
							add(new Button(DictionaryTabel.tishiButton_returnHall, null));
						}
					}) + "@&");
					exiting("金币不足入场限制");
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public boolean isDianKong() {
		try {
			if (playerService != null) {
				if (playerService.getPlayer().getPlayerDiankong() != null) {
					float rate = playerService.getPlayer().getPlayerDiankong().getDianKongRate();
					if (Math.random() > (1 - rate)) {
						return true;
						// 赢的点控
					} else {
						return false;
					}
				} else {
					return false;
				}
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

	public JSONObject faPai(boolean isHaveFree) {
		initPaiMap(isHaveFree);
		JSONObject awardResultJson = getAllAwardInfo();
		long totalFen = awardResultJson.getLong("totalFen");
		System.out.println("发牌时奖金池:" + getRoomDeskService().normalFanFenChi + "@大于等于此值时候随机开牌" + getRoomDeskService().getConfigRoomService().normalRandomSection);
		if (getRoomDeskService().normalFanFenChi < getRoomDeskService().getConfigRoomService().normalRandomSection) {
			if (awardResultJson.getLong("totalFen") == 0) {
				return awardResultJson;
			} else {
				float value = (float) Math.random();
				// System.out.println("分数低于设定值,降低开牌概率,降低概率为" +
				// getRoomDeskService().getConfigRoomService().normalPeiRate);
				// System.out.println(value);
				long myBet = betArea[curBetIndex];
				int curBei = (int) (totalFen / myBet);
				// System.out.println("开奖倍数:" + curBei);
				if (value < getRoomDeskService().getConfigRoomService().normalPeiRate) {
					// System.out.println("受到控制概率");
					if (curBei <= getRoomDeskService().getConfigRoomService().normalBei) {
						// System.out.println("符合开奖倍数");
						if (isDianKong()) {
							return faPai(isHaveFree);
						} else {
							return awardResultJson;
						}
					} else {
						// System.out.println("不符合开奖概率,重新获取");
						return faPai(isHaveFree);
					}
				} else {
					// System.out.println("不符合控制概率");
					if (isDianKong()) {
						return faPai(isHaveFree);
					} else {
						return awardResultJson;
					}
				}
			}
		} else {
			if (isDianKong()) {
				// 输的点控
				if (totalFen == 0) {
					return awardResultJson;
				} else {
					return faPai(isHaveFree);
				}
			} else {
				return awardResultJson;
			}
		}
		// return awardResultJson;
	}

	private boolean isCanFree() {
		Integer[][] hang = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };
		int count = 0;
		for (int i = 0; i < hang.length; i++) {
			Integer[] arr = hang[i];
			for (int j = 0; j < arr.length; j++) {
				int a = arr[j];
				Pai pai = paiMap.get(a);
				if (pai.getType() == 12) {
					count ++;
				}

			}
//			if (isHaveSpAwardByType(arr, 12)) {
//				count++;
//			}
		}
		System.out.println("count"+count);
		if (count >= 4) {
			return true;
		}
		return false;
	}

	private boolean isHaveSpAwardByType(Integer[] arr, int type) {
		boolean flag = false;
		for (int i = 0; i < arr.length; i++) {
			int a = arr[i];
			Pai pai = paiMap.get(a);
			if (pai.getType() == type) {
				flag = true;
				break;
			}

		}
		return flag;
	}

	/*private boolean isCanBONUS() {
		String[][] hang = { { "0", "1", "2" }, { "6", "7", "8" }, { "12", "13", "14" } };
		int count = 0;
		for (int i = 0; i < hang.length; i++) {
			String[] arr = hang[i];
			if (isHaveSpAwardByType(arr, 10)) {
				count++;
			}
		}
		if (count == hang.length) {
			return true;
		}
		return false;
	}*/

	public String getRemark() {
		String result = "";
		for (Map.Entry<Integer, Pai> entry : paiMap.entrySet()) {
			int position = entry.getKey();
			Pai pai = entry.getValue();
			result += position + "," + pai.getName() + ";";
		}
		return result;
	}

	public synchronized void sendGameResult() {
		System.out.println("sendGameResult");
		setGameState(DictionaryTabel.Game_OpenAwarding, false);
		JSONObject awardResultJson = faPai(freeOpenAwardCount <= 0);
		JSONObject jsonObject = new JSONObject();
//		if (isCanBONUS()) {
//			bonusGameFlag = true;
//		} else {
//			bonusGameFlag = false;
//		}
		jsonObject.put("nexFreeFlag", 0);// 下一局正常free
		if (isCanFree()) {
			jsonObject.put("nexFreeFlag", 1);// 下一局开启free
			freeFlag = false;
			freeDealFirst();
		} else {
			freeDeal();
		}
		// 奖励类型
		long totalFen = awardResultJson.getLong("totalFen");
		//long bossXue = awardResultJson.getLong("bossXue");
		playerNowBossFen += playerNowBossFen + totalFen * 0.05;
		playerService.deskPlayer.setPlayerNowBossFen(playerNowBossFen);
		//getRoomDeskService().addNowRankBossXue(bossXue);
		
		if (totalFen / betArea[curBetIndex] > 40) {
			jsonObject.put("awardType", "2");
		} else if (totalFen / betArea[curBetIndex] > 20) {
			jsonObject.put("awardType", "1");
		} else {
			jsonObject.put("awardType", "0");
		}
		//jsonObject.put("bonusGameFlag", bonusGameFlag);
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < paiMap.size(); i ++) {
			list.add(paiMap.get(i).getType());
		}
		jsonObject.put("pai", list);
		// 判断得奖
		jsonObject.put("awardResult", awardResultJson);
		//jsonObject.put("zhuangTime", 1);
		System.out.println("背包："+playerService.getPlayer().getBagInfo().getLong("2"));
		System.out.println("押注："+betArea[curBetIndex]);
		if (!freeFlag) {
			jsonObject.put("curMoney", (playerService.getPlayer().getBagInfo().getLong("2") - betArea[curBetIndex]));
			jsonObject.put("nowMoney", playerService.getPlayer().getBagInfo().getLong("2") - betArea[curBetIndex] + totalFen);
		} else {
			jsonObject.put("curMoney", playerService.getPlayer().getBagInfo().getLong("2"));
			jsonObject.put("nowMoney", playerService.getPlayer().getBagInfo().getLong("2") + totalFen);
		}
		
		//if (freeFlag) {
			jsonObject.put("freeCount", freeOpenAwardCount);
		//} 
//		else {
//			if (autoCount == -1 || autoCount == 0) {
//				jsonObject.put("autoCount", autoCount);
//			} else {
//				jsonObject.put("autoCount", --autoCount);
//			}
//		}
		/*if (isAuto()) {
			if (freeFlag) {
				jsonObject.put("autoCount", freeOpenAwardCount + "/" + totalFreeCount);
			} else {
				if (autoCount == -1 || autoCount == 0) {
					jsonObject.put("autoCount", autoCount);
				} else {
					jsonObject.put("autoCount", --autoCount);
				}
			}
		} else {
			if (freeFlag) {
				setAuto(true);
			}
			jsonObject.put("autoCount", "");
		}*/
		System.out.println("freeOpenAwardCount-----》" + freeOpenAwardCount);
		if (freeOpenAwardCount == 0 || freeOpenAwardCount == -1) {
			freeOpenAwardCount = -1;
		}
		jsonObject.put("freeFlag", freeFlag);
		if ((freeOpenAwardCount == 0 || freeOpenAwardCount == -1) && freeFlag == true) {
			jsonObject.put("nexFreeFlag", 2);// 下一局取消free
		}
		gameResultTimerTask(jsonObject.toString());
	}

	public void gameResultTimerTask(final String resultStr) {
		try {
			System.out.println("resultStr--->" + resultStr);
			// System.out.println(getRemark());
			playerService.sendMessage(SocketEnum.gameResultI, resultStr);
//			Thread.sleep(2500);
			waitDeal();
		} catch (Exception e) {
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
			e.printStackTrace();
		}
	}

	public synchronized void waitDeal() {
			if (curAwardFen == 0) {
				setGameState(DictionaryTabel.Game_Result, false);
			} 
			else {
				quFen();
			}
	}

	public void freeDealFirst() {
		System.out.println("免费开始");
		//freeOpenAwardCount = 10;
		//totalFreeCount = 10;
		//setAuto(true);
	}

	public void freeDeal() {
		if (freeOpenAwardCount >= 0) {
			System.out.println("免费处理：" + freeFlag);
			freeFlag = true;
			freeOpenAwardCount--;
		}
		if (freeOpenAwardCount <= -1) {
			freeFlag = false;
			freeOpenAwardCount = -1;
		}
	}

	public void gameResultWaitTimerTask() {
		// System.out.println("哈哈");
		if (curAwardFen != 0) {
		}
		checkDianKong(1);
		clearData();
		setGameState(DictionaryTabel.Game_Wait, false);
		if (playerService != null) {
			playerService.sendMessage(SocketEnum.gameOverI, "");
		}
	}

	public JSONObject getAllAwardInfo() {
		JSONObject jsonObject = null;
		long totalFen = 0;
		long myBossFen = 0;
		jsonObject = new JSONObject();
		String lineNumber = "";
		for (int i = 0; i < 1; i++) {
			//String xianLuStr = yaXianMap.get(i + "");
			//String[] xianLuArr = xianLuStr.split(",");
			ArrayList<Pai> paiList = new ArrayList<>();
			for (int j = 0; j < 9; j++) {
				Pai pai = paiMap.get(j);
				paiList.add(pai);
			}
			JSONObject oneLineAwardInfo = getOneLineAwardInfo(paiList);
			if (!oneLineAwardInfo.getString("info").equals("")) {
				String[] arr = oneLineAwardInfo.getString("info").split(",");
				String index = arr[0];
				int count = Integer.valueOf(arr[1]);
				long curXianTotalFen = Long.valueOf(arr[2]);
				long curXianBossFen = Long.valueOf(arr[3]);
				jsonObject.put("lineNumber", oneLineAwardInfo.get("lineNumber"));
				totalFen += curXianTotalFen;
				myBossFen += curXianBossFen;
				String result = "";
				if (index.equals("1")) {
//					for (int m = 0; m < count; m++) {
//						if (m != count - 1) {
//							result += xianLuArr[m] + ",";
//						} else {
//							result += xianLuArr[m];
//						}
//					}
				}
//				if (!result.equals("")) {
//					lineNumber += result + "#" + i + ";";
//				}
			}
		}
		//jsonObject.put("lineNumber", lineNumber);
		curAwardFen = totalFen;
		bossFenT = myBossFen;
		if (totalFen > 0) {
			jsonObject.put("isAward", true);
		} else {
			jsonObject.put("isAward", false);
		}
		jsonObject.put("totalFen", totalFen);
		jsonObject.put("myBossFen", myBossFen);
		jsonObject.put("myBossFenTotal", playerService.deskPlayer.getPlayerNowBossFen());
		return jsonObject;
	}

	public boolean biJiaoType(Pai pai, Pai paiT) {
		/*if ((pai.getType() == 11 || paiT.getType() == 11) && pai.getType() != 10 && pai.getType() != 12 && paiT.getType() != 10 && paiT.getType() != 12) {
			return true;
		}*/
		if (pai.getType() == paiT.getType()) {
			return true;
		}
		return false;
	}

	
	public double judgeBossXue(double singleFen,int paiType) {
		double bossXue = 0l;
		if (getRoomDeskService().configDesk.getNowRank() == 0) {
			if (paiType == 0 || paiType == 1 || paiType == 2) {
				bossXue = singleFen;
			}
		}
		if (getRoomDeskService().configDesk.getNowRank() == 1) {
			if (paiType == 3 || paiType == 4 || paiType == 5) {
				bossXue = singleFen;
			}
		}
		if (getRoomDeskService().configDesk.getNowRank() == 2) {
			if (paiType == 6 || paiType == 7 || paiType == 8) {
				bossXue = singleFen;
			}
		}
		return bossXue;
		
	}
	public JSONObject getOneLineAwardInfo(ArrayList<Pai> paiList) {
		JSONObject jsonObject = new JSONObject();
		JSONObject leftObj = getLeftAwardInfo(paiList);
		if (leftObj.get("info").equals("")) {
			jsonObject.put("info", "");
			jsonObject.put("type", "");
		} else {
			jsonObject = leftObj;
		}
		return jsonObject;
	}

	public void clearData() {
		curAwardFen = 0;
		//bonusFen = 0;
		// isFirst = true;
	}

	boolean isJieSuan = false;

	public void game_BONUSWait() {
		JSONObject json = new JSONObject();
		long fenRewardTotal = 0;
		DeskPlayerService[] deskPlayerServices = getRoomDeskService().deskPlayerServices;
		for (DeskPlayerService dps : deskPlayerServices) {
			json.put(dps.playerService.deskPlayer.getPlayerId(), dps.playerService.getPlayer().getPlayerNowBossFen());
			fenRewardTotal += dps.playerService.getPlayer().getPlayerNowBossFen();
			
		}
		playerService.sendMessage(SocketEnum.fenReward, json.toString());
		for (DeskPlayerService dps : deskPlayerServices) {
			game_FenReward(dps,fenRewardTotal);
		}
		setGameState(DictionaryTabel.Game_Wait, false);
	}
	public void game_FenReward(DeskPlayerService dps,long fenRewardTotal) {
		// TODO Auto-generated method stub
		if (isJieSuan == true) {
			return;
		}
		isJieSuan = true;
		if (playerService != null) {
			try {
				long playerBossFen = dps.playerService.getPlayer().getPlayerNowBossFen();
				long fenReward = playerBossFen/fenRewardTotal * getRoomDeskService().getConfigDesk().getBossTotalXue();
				String ss = "BOSS得分:" + fenReward + "@押注:" + betArea[curBetIndex] + "@免费:" + freeFlag + "@";
					BeanFactory.PlayerService.addJingDianJieSuan(dps.playerService, this, fenReward, 0,
							betArea[curBetIndex] + "@", ss + getRemark());
				dps.playerService.getPlayer().setPlayerNowBossFen(0);
			} catch (Exception e) {
				clearData();
				setGameState(DictionaryTabel.Game_Sleep, false);
				// TODO: handle exception
				e.printStackTrace();
			}
		} else {
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
		}
	}
	
	public synchronized void game_Result() {
		System.out.println("isJieSuan:"+isJieSuan);
		if (isJieSuan == true) {
			return;
		}
		isJieSuan = true;
		if (playerService != null) {
			try {
				long bei = curAwardFen / (betArea[curBetIndex]);
				if (bei > 20 && curAwardFen > 1000000) {
					if (bei >= getRoomDeskService().getConfigRoomService().beiBroadcast) {
						String broadcastContent = Rule.getBroadcast(getRoomDeskService().getConfigRoomService().contentBroadcast, playerService.getPlayer()
								.getNickName(), getRoomDeskService().getConfigRoomService().roomName, bei + "倍奖励", curAwardFen + "");
						DataService.sendSms(broadcastContent);
					}
				}
				long normalFanFen = curAwardFen;
				long bossXue = bossFenT;
				getRoomDeskService().addNormalFanFenChi(-normalFanFen);
				getRoomDeskService().addNowRankBossXue(bossXue);
				// System.out.println("投注消耗的金币:" + myTotalBet);
				// System.out.println("压线获得的金币:" + curAwardFen);
				// System.out.println("Bonus中奖金币:" + bonusFen);
				// System.out.println("结算的金币:" + (curAwardFen - myTotalBet +
				// bonusFen));
				String ss = "得分:" + curAwardFen + "@押注:" + betArea[curBetIndex] + "@免费:" + freeFlag + "@";
				if (freeFlag) {
					BeanFactory.PlayerService.addJingDianJieSuan(playerService, this, curAwardFen, 0,
							betArea[curBetIndex] + "@" + myYaXian, ss + getRemark());
				} else {
					BeanFactory.PlayerService.addJingDianJieSuan(playerService, this, curAwardFen - betArea[curBetIndex], 0,
							betArea[curBetIndex] + "@" + myYaXian, ss + getRemark());
				}
				gameResultWaitTimerTask();
			} catch (Exception e) {
				clearData();
				setGameState(DictionaryTabel.Game_Sleep, false);
				// TODO: handle exception
				e.printStackTrace();
			}
		} else {
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
		}
	}

	// public void game_AwardWait() {
	// // TODO Auto-generated method stub
	// if (isAuto()) {
	// quFen();
	// }
	// }

	public void addGameRecord(int massageHead, String messageContent) {
		getRoomDeskService().getConfigDesk().setGameRecord(getRoomDeskService().getConfigDesk().getGameRecord() + massageHead + "@" + getRoomDeskService().getConfigDesk().getRecordRunTime() + "@" + messageContent + "@&");
	}

	public void clearEndJu() {
		getRoomDeskService().getConfigDesk().setGameRecord("");
		if (playerService != null) {
			try {
				if (playerService.getPlayer().getDeskNum() == getRoomDeskService().getConfigDesk().getDeskNum()) {
					playerService.clearEndJu();
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				clearData();
				setGameState(DictionaryTabel.Game_Sleep, false);
			}
		}
	}

//	public void resetDesk(String s) {
//		setGameState(DictionaryTabel.Game_Sleep, false);
//		if (playerService != null) {
//			try {
//				playerService.sendMessage(SocketEnum.infoI, Constant.Tishi_Param_Error);
//			} catch (Exception e) {
//				// TODO: handle exception
//				e.printStackTrace();
//				clearData();
//				setGameState(DictionaryTabel.Game_Sleep, false);
//			}
//		}
//		exiting("重置");
//	}

//	public void closeDesk() {
//		getDesk().setClosing(false);
//		String messageStrs = Constant.getTishiT(getDesk().getCloseDeskMessage(), 1, new ArrayList<Button>() {
//			{
//				add(new Button(DictionaryTabel.tishiButton_returnHall, null));
//			}
//		});
//		if (playerService != null) {
//			try {
//				playerService.sendMessage(SocketEnum.infoI, messageStrs);
//				exit("desk close");
//			} catch (Exception e) {
//				// TODO: handle exception
//				e.printStackTrace();
//				clearData();
//				setGameState(DictionaryTabel.Game_Sleep, false);
//			}
//		}
//		clearEndJu();
//		setGameState(DictionaryTabel.Game_Close, false);
//	}

//	public Desk getDesk() {
//		return desk;
//	}
//
//	public void setDesk(Desk desk) {
//		this.desk = desk;
//	}

	public RoomDeskService getRoomDeskService() {
		return roomDeskService;
	}

	public void setRoomDeskService(RoomDeskService roomDeskService) {
		this.roomDeskService = roomDeskService;
	}

	public synchronized void quFen() {
		try {
			if (getRoomDeskService().getConfigDesk().getGameState() == DictionaryTabel.Game_OpenAwarding) {
				// playerService.sendMessages(SocketEnum.quFenI,
				// curAwardFen + "");
				setGameState(DictionaryTabel.Game_Result, false);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error("commonExceotion", e);
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
		}
	}

	// ===================维护=================

	/*public void resetDesk(String messageStrs, boolean isNow) {
		try {
			getDesk().setCloseDeskMessage(messageStrs);
			if (isNow) {
				resetDesk_now();
			} else {
				if (playerService == null) {
					resetDesk_now();
				} else {
					getDesk().setReset(true);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("commonExceotion", e);
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
		}
	}

	public void resetDesk_now() {
		String mm = Constant.getTishiT(getDesk().getCloseDeskMessage(), 1, new ArrayList<Button>() {
			{
				add(new Button(DictionaryTabel.tishiButton_sure, null));
			}
		});
		if (playerService != null) {
			try {
				playerService.sendMessage(SocketEnum.returnHallI, mm);
				exiting("weihuClose");
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				clearData();
				setGameState(DictionaryTabel.Game_Sleep, false);
			}
		}
		setGameState(DictionaryTabel.Game_Sleep, false);
		getDesk().setGameTime(0);

	}

	public void choseDesk(String messageStrs, boolean isNow) {
		try {
			getDesk().setCloseDeskMessage(messageStrs);
			if (isNow) {
				closeDesk_now();
			} else {
				getDesk().setClosing(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("commonExceotion", e);
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
		}
	}

	public void closeDesk_now() {
		try {
			String mm = Constant.getTishiT(getDesk().getCloseDeskMessage(), 1, new ArrayList<Button>() {
				{
					add(new Button(DictionaryTabel.tishiButton_sure, null));
				}
			});
			PlayerService playerService = playerService;
			if (playerService != null) {
				playerService.sendMessage(SocketEnum.returnHallI, mm);
				exiting("weihuClose");
			}
			setGameState(DictionaryTabel.Game_Close, false);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("commonExceotion", e);
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
		}
	}*/

	/** 玩家操作 **/
	public synchronized void begin(String content) {
		System.out.println("游戏状态"+getRoomDeskService().getConfigDesk().getGameState());
		System.out.println("游戏状态111"+roomDeskService.getConfigDesk().getGameState());
		try {
			//JSONObject paramObj = JSONObject.fromObject(content);
			//int isAutoT = paramObj.getInt("isAuto");
			if (getRoomDeskService().getConfigDesk().getGameState() == DictionaryTabel.Game_Wait) {
					if (!freeFlag) {
						curBetIndex = Integer.parseInt(content);
					}
					System.out.println("当前档次："+ curBetIndex);
					if (freeOpenAwardCount <= 0 && playerService.getPlayer().getMoney() - betArea[curBetIndex] < 0) {
						playerService.sendMessage(SocketEnum.infoI, Constant.Tishi_MoneyNo_Full);
						return;
					}
					game_beginJu();
				}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
			logger.error("commonExceotion", e);
		}
		
	}


	public void setGameState(int value, boolean isDic) {
		getRoomDeskService().getConfigDesk().setGameState(value);
		if (isDic) {
			deal();
		}
	}

	/** -------------------------------点控---------------------------- **/

	public void checkDianKong(int flag) {
		// 每局开始检查是否需要设置点控参数
		PlayerService playerDeskService = playerService;
		if (playerService != null) {
			if (playerDeskService.getPlayer().getPlayerDiankong() == null) {// 若当前没有点控信息，查询是否有点控
				PlayerDiankong playerDiankong = BeanFactory.PlayerService.getDianKongInfoLast(Long.valueOf(playerDeskService.getPlayer().getPlayerId()));
				if (playerDiankong != null) {
					playerDeskService.getPlayer().setPlayerDiankong(playerDiankong);
				}
			} else {// 如果有点控信息，判断点控是否结束
				PlayerDiankong playerDiankong = playerDeskService.getPlayer().getPlayerDiankong();
				if (BeanFactory.PlayerService.getDianKongInfoById(playerDiankong.getId()) == null) {
					playerDeskService.getPlayer().setPlayerDiankong(null);
					return;
				}
				if (playerDiankong.getTargetFen() < 0) {// 分数减少的点控
					if (playerDiankong.getFinishFen() < playerDiankong.getTargetFen()) {// 点控完成后处理
						playerDiankong.setIsFinish(1);
						playerDiankong.setFinishTime(new Timestamp(System.currentTimeMillis()));
						BeanFactory.PlayerService.updateDianKongInfo(playerDiankong);
						playerDeskService.getPlayer().setPlayerDiankong(null);
					}
				} else if (playerDiankong.getTargetFen() > 0) {
					if (playerDiankong.getFinishFen() > playerDiankong.getTargetFen()) {// 点控完成后处理
						playerDiankong.setIsFinish(1);
						playerDiankong.setFinishTime(new Timestamp(System.currentTimeMillis()));
						BeanFactory.PlayerService.updateDianKongInfo(playerDiankong);
						playerDeskService.getPlayer().setPlayerDiankong(null);
					}
				}
			}
			PlayerDiankong playerDiankong = playerDeskService.getPlayer().getPlayerDiankong();
			if (playerDiankong != null) {
				if (flag == 0) {
					playerDiankong.setIsInWork(1);
				} else if (flag == 1) {
					playerDiankong.setIsInWork(0);
				}
				BeanFactory.PlayerService.updateDianKongInfo(playerDiankong);
			}
		}
	}

	public void finishDiankong(PlayerService ps, PlayerDiankong diankong) {
		try {
			ps.getPlayer().setPlayerDiankong(null);
			diankong.setIsFinish(1);
			diankong.setFinishTime(new Timestamp(System.currentTimeMillis()));
			ps.getdiankong(ps.getPlayer().getPlayerId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("commonExceotion", e);
			clearData();
			setGameState(DictionaryTabel.Game_Sleep, false);
		}
	}

	public String getDeskInfo() {
		String deskInfo = "";
		deskInfo += "ttt******************************ttt";
		long sysShuYing = getRoomDeskService().chouShuiChi + getRoomDeskService().normalFanFenChi;
		deskInfo += "系统总输赢:" + sysShuYing + "ttt";
		deskInfo += "抽水池：" + getRoomDeskService().chouShuiChi + "    当前抽水概率 ：    " + getRoomDeskService().getConfigRoomService().chouShuiChiRate + "ttt";
		deskInfo += "正常返分池：" + getRoomDeskService().normalFanFenChi + "    ||  当前正常返分概率：   " + getRoomDeskService().getConfigRoomService().normalFanFenRate + "ttt";
		deskInfo += "ttt******************************ttt";
		return deskInfo;
	}

	/** -------------------------------结束---------------------------- **/


	public JSONObject getLeftAwardInfo(ArrayList<Pai> paiList) {
		JSONObject jsonObject = new JSONObject();
		Map<Integer, Integer> map=new HashMap<Integer, Integer>();
		String info = "";
		int count = 0;
		long totalFen = 0;
		double singleFen = 0;
		long bossXue = 0;
		List<Integer> lineNumber = new ArrayList<>();
		//paiList.contains("wild");
		Set<Integer> set = new HashSet<Integer>();
		for (Pai i : paiList) {
			set.add(i.getType());
		}
		if (set.size() > 7) {
			jsonObject.put("info", "");
			jsonObject.put("type", "");
			return jsonObject;
		}else {

			Collections.sort(paiList, new Comparator<Pai>() {
				@Override
				public int compare(Pai o1, Pai o2) {
					
					return o1.getType()-o2.getType();
				}
	        });
			Pai paiT = paiList.get(0);
			for (int i = 0; i < paiList.size(); i++) {
				if (paiT.getType() != paiList.get(i).getType()) {
					paiT = paiList.get(i);
					count = 1;
				}else {
					count ++;
				}
				map.put(paiList.get(i).getType(), count);
			}
			ArrayList<Pai> paiList1 = FactoryPai.getInstance().paiList;
			for (Integer o : map.keySet()) {


				if (map.get(o) >= 4) {

					switch (map.get(o)) {
					case 4:
						lineNumber.add(o);
						if (o == 12) {
							freeOpenAwardCount = 10;
							totalFreeCount = 10;
						}else {
							if (map.containsKey(9) || map.containsKey(10)
									|| map.containsKey(11)) {
								for (Integer key : map.keySet()) {
									switch (key) {
									case 9:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x4 * 2;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 10:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x4 * 3;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 11:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x4 * 5;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									default:
										break;
									}
								}
							} else {
								singleFen = betArea[curBetIndex] / betArea[0]
										* paiList1.get(o).x4;
								totalFen += singleFen;
								bossXue += judgeBossXue(singleFen,o);
								
							}
						}
						break;
					case 5:
						lineNumber.add(o);
						if (o == 12) {
							freeOpenAwardCount = 10;
							totalFreeCount = 10;
						}else {
							if (map.containsKey(9) || map.containsKey(10)
									|| map.containsKey(11)) {
								for (Integer key : map.keySet()) {
									switch (key) {
									case 9:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x5 * 2;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 10:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x5 * 3;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 11:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x5 * 5;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									default:
										break;
									}
								}
							} else {
								singleFen = betArea[curBetIndex] / betArea[0]
										* paiList1.get(o).x5;
								totalFen += singleFen;
								bossXue += judgeBossXue(singleFen,o);
							}
						}
						break;
					case 6:
						lineNumber.add(o);
						if (o == 12) {
							freeOpenAwardCount = 15;
							totalFreeCount = 15;
						}else {
							if (map.containsKey(9) || map.containsKey(10)
									|| map.containsKey(11)) {
								for (Integer key : map.keySet()) {
									switch (key) {
									case 9:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x6 * 2;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 10:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x6 * 3;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 11:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x6 * 5;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									default:
										break;
									}
								}
							} else {
								singleFen = betArea[curBetIndex] / betArea[0]
										* paiList1.get(o).x6;
								totalFen += singleFen;
								bossXue += judgeBossXue(singleFen,o);
							}
						}
						break;
					case 7:
						lineNumber.add(o);
						if (o == 12) {
							freeOpenAwardCount = 15;
							totalFreeCount = 15;
						}else {
							if (map.containsKey(9) || map.containsKey(10)
									|| map.containsKey(11)) {
								for (Integer key : map.keySet()) {
									switch (key) {
									case 9:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x7 * 2;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 10:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x7 * 3;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 11:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x7 * 5;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									default:
										break;
									}
								}
							} else {
								singleFen = betArea[curBetIndex] / betArea[0]
										* paiList1.get(o).x7;
								totalFen += singleFen;
								bossXue += judgeBossXue(singleFen,o);
							}
						}
						break;
					case 8:
						lineNumber.add(o);
						if (o == 12) {
							freeOpenAwardCount = 15;
							totalFreeCount = 15;
						}else {
							if (map.containsKey(9) || map.containsKey(10)
									|| map.containsKey(11)) {
								for (Integer key : map.keySet()) {
									switch (key) {
									case 9:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x8 * 2;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 10:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x8 * 3;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 11:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x8 * 5;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									default:
										break;
									}
								}
							} else {
								singleFen = betArea[curBetIndex] / betArea[0]
										* paiList1.get(o).x8;
								totalFen += singleFen;
								bossXue += judgeBossXue(singleFen,o);
							}
						}
						break;
					case 9:
						lineNumber.add(o);
						if (o == 12) {
							freeOpenAwardCount = 15;
							totalFreeCount = 15;
						}else {
							if (map.containsKey(9) || map.containsKey(10)
									|| map.containsKey(11)) {
								for (Integer key : map.keySet()) {
									switch (key) {
									case 9:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x9 * 2;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 10:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x9 * 3;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									case 11:
										singleFen = betArea[curBetIndex]
												/ betArea[0]
												* paiList1.get(o).x9 * 5;
										totalFen += singleFen;
										bossXue += judgeBossXue(singleFen,o);
										break;
									default:
										break;
									}
								}
							} else {
								singleFen = betArea[curBetIndex] / betArea[0]
										* paiList1.get(o).x9;
								totalFen += singleFen;
								bossXue += judgeBossXue(singleFen,o);
							}
						}
						break;
					}
					// info = "1," + count + "," + totalFen;

				} else if (map.get(o) == 3
						&& (o == 0 || o == 1 || o == 2 || o == 3 || o == 4)) {
					lineNumber.add(o);
					if (map.containsKey(9) || map.containsKey(10)
							|| map.containsKey(11)) {
						for (Integer key : map.keySet()) {
							switch (key) {
							case 9:
								singleFen = betArea[curBetIndex] / betArea[0]
										* paiList1.get(o).x3 * 2;
								totalFen += singleFen;
								bossXue += judgeBossXue(singleFen,o);
								break;
							case 10:
								singleFen = betArea[curBetIndex] / betArea[0]
										* paiList1.get(o).x3 * 3;
								totalFen += singleFen;
								bossXue += judgeBossXue(singleFen,o);
								break;
							case 11:
								singleFen = betArea[curBetIndex] / betArea[0]
										* paiList1.get(o).x3 * 5;
								totalFen += singleFen;
								bossXue += judgeBossXue(singleFen,o);
								break;
							default:
								break;
							}
						}
					} else {
						singleFen = betArea[curBetIndex] / betArea[0]
								* paiList1.get(o).x3;
						totalFen += singleFen;
						bossXue += judgeBossXue(singleFen,o);
					}
				}
			
			}
			info = "1," + count + "," + totalFen + "," + bossXue;
		}
		
		jsonObject.put("totalFen", totalFen);
		jsonObject.put("bossXue", bossXue);
		jsonObject.put("lineNumber", lineNumber);
		jsonObject.put("info", info);
		
		//jsonObject.put("type", paiT.getType() + "");
		return jsonObject;
	}
}
